package com.lsh.airticket.controller;

import com.lsh.airticket.dto.Result;
import com.lsh.airticket.dto.SearchFlight;
import com.lsh.airticket.dto.ShowFlight;
import com.lsh.airticket.entity.User;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lsh
 * @date 2021/2/15
 **/
@RestController
@RequestMapping("/flights")
public class FlightController {

    @Autowired
    FlightService flightService;

    /**
     * 查询航班
     *
     * @param searchFlight
     * @return
     */
    @GetMapping("/flight/searchFight")
    public Result searchFight(SearchFlight searchFlight) {
        if (searchFlight == null || searchFlight.getTime() == null) {
            throw new UserException("请输入");
        }
        if ((searchFlight.getFromCity() == null || searchFlight.getToCity() == null) && searchFlight.getFlightId() == null) {
            throw new UserException("请输入");
        }
        System.out.println("searchFlight=============" + searchFlight);
        List<ShowFlight> flights = null;
        try {
            flights = flightService.findFlight(searchFlight);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UserException("查询失败");
        }
        return Result.result().setMsg("查询成功").addData("flights", flights).addData("resultSearch", searchFlight);
    }
}
